/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pa165.dominatingspecies.frontend.element;

import org.apache.wicket.Component;
import org.apache.wicket.model.PropertyModel;
import org.apache.wicket.util.lang.PropertyResolver;

/**
 *
 * @author Ondrej Prazak
 */
public class EnumPropertyModel<T extends Enum<T>> extends PropertyModel<String> {

    private Component component;

    public EnumPropertyModel(Object modelObject, String expr, Component resourceProvider) {
        super(modelObject, expr);
        this.component = resourceProvider;
    }

    @Override
    @SuppressWarnings("unchecked")
    public String getObject() {
        final String expr = propertyExpression();
        final Object target = getTarget();
        if (target != null) {
            T enumValue = (T) PropertyResolver.getValue(expr, target);
            String key = EnumKeyProvider.getKey(enumValue);
            return component.getString(key);
        }
        return null;
    }
}
